home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / circuits / irsim-ca.2 / irsim-ca / irsim-cap-9.2 / src / utils / Makedep / makedep.c < prev    next >
C/C++ Source or Header  |  1990-12-23  |  7KB  |  299 lines

  1. /*
  2.  * Program to construct dependency lines for makefiles.
  3.  *
  4.  * Marvin Theimer and Tim Mann, 3/18/84.
  5.  *
  6.  * Synopsis
  7.  *    makedep [options] [source files]
  8.  *
  9.  * Discussion
  10.  *   Makedep constructs a makefile-style dependency list
  11.  *   showing which header files the object files constructed
  12.  *   from the given source files depend
  13.  *   upon.  The dependency of the object file upon the source 
  14.  *   file is not indicated in the output; this dependency can 
  15.  *   usually be inferred by the make program.
  16.  *
  17.  *   Makedep handles nested includes properly, propagating
  18.  *   dependencies of one header file upon another back to
  19.  *   each object file whose source file includes the dependent
  20.  *   header file.
  21.  *
  22.  * Options
  23.  *   -o outfile
  24.  *    Output file name.  The default is "dependencies".  The name "-" 
  25.  *    indicates standard output.
  26.  *
  27.  *   -I dir
  28.  *    Add "dir" to the include file search list.  Multiple
  29.  *    -I options accumulate, building the search list from
  30.  *    left to right, with the system include directories
  31.  *    added at the end.  The space separating the directory
  32.  *    name from the -I may be omitted.  Directory names
  33.  *    may be given relative to the directory from which
  34.  *    makedep is invoked.  
  35.  *
  36.  *   -U
  37.  *    Use the standard Unix header directories as the system
  38.  *    search list.  Equivalent to specifying -I/usr/include after
  39.  *    all other -I options.
  40.  *
  41.  *   -V
  42.  *    Use the standard V-System header directories as the
  43.  *    system search list.  Equivalent to specifying the options
  44.  *    -I/usr/sun/include -I/usr/local/include -I/usr/include after
  45.  *    all other -I options.
  46.  *
  47.  *   -xV
  48.  *    Use the experimental V-System header directories as the
  49.  *    system search list.  Equivalent to specifying the options
  50.  *    -I/usr/sun/xinclude -I/usr/sun/include -I/usr/local/include
  51.  *    -I/usr/include after all other -I options.
  52.  *
  53.  *   -N
  54.  *    Use no system search list.  Suppresses the warning message 
  55.  *    ordinarily printed when a header file cannot be found.  This
  56.  *    option is useful when you are not interested in dependencies
  57.  *    on system include files.
  58.  *
  59.  *   -e ext
  60.  *    Object files have extension ".ext".  Defaults to .b if -V or -xV
  61.  *    is specified, .o otherwise.
  62.  *
  63.  *   -d
  64.  *    Turn on debug output.  Useful only to the maintainers.
  65.  *
  66.  *   If the source files depend on any header files in standard system 
  67.  *   include directories, one of the options -U, -V, -xV, or -N should 
  68.  *   normally be specified.  These four options are mutually exclusive.
  69.  *   If none of these options is given, only the directories specified 
  70.  *   in -I options are included in the search list (as with the -N flag), 
  71.  *   but warning messages are still printed for any header files that
  72.  *   cannot be found.
  73.  */
  74.  
  75. #include "makedep.h"
  76.  
  77. extern FILE *freopen();
  78.  
  79.  
  80. main(argc, argv)
  81.     int argc;
  82.     char **argv;
  83.   {
  84.  
  85.     Debug = 0;
  86.  
  87.     Initialize();
  88.     ProcessCommandLine(argc, argv);
  89.     /* Redirect stdout to the output file. */
  90.     if (strcmp(OutputFileName, "-") != 0)
  91.        {
  92.         if (freopen(OutputFileName, "w", stdout) == NULL)
  93.           {
  94.         fprintf(stderr, "%s: can't open %s for writing.\n", 
  95.         MyName, OutputFileName);
  96.         perror(OutputFileName);
  97.         exit(errno);
  98.       }
  99.       }
  100.     GenIncludeFileDependencies();
  101.     PrintDependencies();
  102.  
  103.     exit(0);
  104.   }
  105.  
  106.  
  107. /*
  108.  * Initialize various program data structures and variables.
  109.  */
  110.  
  111. Initialize()
  112.   {
  113.  
  114.     /* Set up default option flag settings. */
  115.     NFlag = FALSE;
  116.     UFlag = FALSE;
  117.     VFlag = FALSE;
  118.     xVFlag = FALSE;
  119.     eFlag = FALSE;
  120.  
  121.     /* Set up default source and object extensions. */
  122.     strcpy(ObjExt, DefaultObjExt);
  123.  
  124.     /* Set up default output filename. */
  125.     strcpy(OutputFileName, DefaultOutputFileName);
  126.  
  127.     /* Set up default search list for include files. */
  128.     InclDirs = MakeList();
  129.     UserInclDirs = MakeList();    /* Empty to begin with. */
  130.  
  131.     /* Set up default source directory list. */
  132.     SrcFiles = MakeList();
  133.   }
  134.  
  135.  
  136. /*
  137.  * ProcessCommandLine:
  138.  * Process user's command line.
  139.  */
  140.  
  141. ProcessCommandLine(argc, argv)
  142.     int argc;
  143.     char **argv;
  144.   {
  145.     int nArg = 1;
  146.     char *p;
  147.  
  148.     MyName = argv[0];
  149.  
  150.     /* Parse command line options. */
  151.     while (nArg < argc)
  152.       {
  153.         if ( argv[nArg][0] == '-' )
  154.       {
  155.         switch (argv[nArg][1] )
  156.           {
  157.                 case 'o':
  158.             if (argv[nArg][2] == '\0')
  159.               {
  160.             p = argv[nArg+1];
  161.                 /* Name is a separate input arg. */
  162.             nArg++;    /* Incr. over the input arg. */
  163.               }
  164.             else
  165.               {
  166.             p = &(argv[nArg][2]);
  167.                 /* Name is tacked onto the -o directly. */
  168.               }
  169.             strcpy(OutputFileName, p);
  170.             break;
  171.  
  172.             case 'I':
  173.             if (argv[nArg][2] == '\0')
  174.               {
  175.             AddList(argv[nArg+1], UserInclDirs);
  176.                 /* Name is a separate input arg. */
  177.             nArg++;    /* Incr. over the input arg. */
  178.               }
  179.             else
  180.               {
  181.             AddList(&(argv[nArg][2]), UserInclDirs);
  182.                 /* Name is tacked onto the -I directly. */
  183.               }
  184.                   break;
  185.  
  186.             case 'N':
  187.             NFlag = TRUE;
  188.                   break;
  189.  
  190.             case 'U':
  191.             UFlag = TRUE;
  192.             AddDefaultDirectoryLists(DefaultUnixInclDirs, InclDirs);
  193.                   break;
  194.  
  195.         case 'V':
  196.             VFlag = TRUE;
  197.             AddDefaultDirectoryLists(DefaultVInclDirs, InclDirs);
  198.             if (!eFlag) strcpy(ObjExt, DefaultVObjExt);
  199.             break;
  200.  
  201.             case 'x':  /* xV */
  202.             if (argv[nArg][2] != 'V') 
  203.             goto badswitch;  /* sorry, Edsger */
  204.             xVFlag = TRUE;
  205.             AddDefaultDirectoryLists(DefaultXVInclDirs, InclDirs);
  206.             if (!eFlag) strcpy(ObjExt, DefaultVObjExt);
  207.                   break;
  208.  
  209.             case 'e':
  210.             eFlag = TRUE;
  211.             if (argv[nArg][2] == '\0')
  212.               {
  213.             p = argv[nArg+1];
  214.                 /* Name is a separate input arg. */
  215.             nArg++;    /* Incr. over the input arg. */
  216.               }
  217.             else
  218.               {
  219.             p = &(argv[nArg][2]);
  220.                 /* Name is tacked onto the -e directly. */
  221.               }
  222.             strcpy(ObjExt, p);
  223.                   break;
  224.  
  225.         case 'd':
  226.             Debug = TRUE;
  227.             break;
  228.  
  229.          default:
  230.         badswitch:
  231.             fprintf(stderr, "%s: Unknown switch: %s\n", 
  232.             MyName, argv[nArg]);
  233.             exit(1);
  234.           }
  235.       }
  236.     else            /* No more options */
  237.       {
  238.         break;
  239.       }
  240.     nArg++;
  241.       }
  242.  
  243.     if (NFlag + UFlag + VFlag + xVFlag > 1)
  244.       {
  245.     fprintf(stderr, "%s: -N, -U, -V, and -xV are mutually exclusive.\n",
  246.         MyName);
  247.     exit(1);
  248.       }
  249.  
  250.     /* Add source files for which dependencies are to be found. */
  251.     while (nArg < argc)
  252.       {
  253.     AddList(argv[nArg], SrcFiles);
  254.     nArg++;
  255.       }
  256.  
  257.     /* Merge the list of user include directories with the list of "regular"
  258.        include directories to get the final search path. */
  259.     MergeLists(UserInclDirs, InclDirs);
  260.  
  261.     if (Debug)
  262.       {
  263.     printf("NFlag: %d, UFlag: %d, VFlag: %d, xVFlag",
  264.         NFlag, UFlag, VFlag, xVFlag);
  265.     printf("    ObjExt: %s\n", ObjExt);
  266.     printf("output filename: %s\n", OutputFileName);
  267.     PrintList(SrcFiles, "SrcFiles");
  268.     PrintList(InclDirs, "InclDirs");
  269.     PrintList(UserInclDirs, "UserInclDirs");
  270.       }
  271.   }
  272.  
  273.  
  274. /*
  275.  * AddDefaultDirectoryLists:
  276.  * Adds the directory names in dirs to the list l.
  277.  * The names in dirs are separated by blanks.
  278.  */
  279.  
  280. AddDefaultDirectoryLists(dirs, l)
  281.     char *dirs;
  282.     StringList l;
  283.   {
  284.     char *p, *p1;
  285.  
  286.     p = dirs;
  287.     while (*p != '\0')
  288.       {
  289.     for (p1 = p; ((*p1 != ' ') && (*p1 != '\0')); p1++)
  290.         ;
  291.     while (*p1 == ' ')
  292.       {
  293.         *p1++ = '\0';
  294.       }
  295.     AddList(p, l);
  296.     p = p1;
  297.       }
  298.   }
  299.